package com.cangmaomao.m_mePen.presenter

import com.cangmaomao.lib.config.SUCCESS
import com.cangmaomao.m_mePen.api.ConfigApi
import com.cangmaomao.m_mePen.bean.Login
import com.cangmaomao.m_mePen.contract.RegContract
import com.cangmaomao.network.request.HttpManage
import com.cangmaomao.network.request.RxHttpMange
import com.cangmaomao.network.request.utils.RxSchedulers

class RegPresenter(val view: RegContract.View) : RegContract.Presenter {

    override fun start() {
        loadLogin()
    }

    init {
        view.setPresenter(this)
    }

    override fun loadLogin() {
        RxHttpMange.getInstance().add(view.context().javaClass.name, HttpManage.getInstance()
                .create(ConfigApi::class.java)
                .register(view.loadMap())
                .compose(RxSchedulers.io_main())
                .subscribe({ handleData(it) }, { view.requestFail(it) }))
    }

    override fun getVerificationCode() {
        RxHttpMange.getInstance().add(view.context().javaClass.name, HttpManage.getInstance()
                .create(ConfigApi::class.java)
                .verificationCode(view.phone())
                .compose(RxSchedulers.io_main())
                .subscribe({
                    handleData(it)
                }, {
                    view.requestFail(it)
                }))
    }


    override fun handleData(data: Login) {
        if (data.msg == SUCCESS)
            view.responseData(true, "注册成功!")
        else
            view.responseData(false, data.msg)
    }


}